/**
 * Created With IntelliJ IDEA
 * Description:牛客网：BM24 二叉树的中序遍历
 * https://www.nowcoder.com/practice/0bf071c135e64ee2a027783b80bf781d?tpId=295&tqId=1512964&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj
 * User: DELL
 * Data: 2022-11-25
 * Time: 23:38
 */
import java.util.*;


class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}


public class Solution {

    public void inorder(List<Integer> list,TreeNode root) {
        //遇到空节点则返回
        if (root == null) {
            return;
        }
        //遍历左结点
        inorder(list,root.left);
        //访问根节点
        list.add(root.val);
        //遍历右结点
        inorder(list,root.right);
        return;
    }

    public int[] inorderTraversal (TreeNode root) {
        //创建链表
        List<Integer> list = new ArrayList<>();
        //进行后序遍历
        inorder(list,root);
        //创建数组，存放遍历后的数据
        int[] res = new int[list.size()];
        //将链表的元素放入数组中
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}